package hotel.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import hotel.entity.Bed;
import hotel.entity.Category;
import hotel.service.IBedService;
import hotel.service.ICategoryService;
import util.JSONUtil;

@Controller
public class CategoryController {
	// Jackson
	private final static ObjectMapper mapper = new ObjectMapper();
	@Autowired
	private ICategoryService<Category> categoryService;
	@Autowired
	private IBedService<Bed> bedService;
	
	@RequestMapping(value="categoryManagement")
	public String categoryManagement() {
		return "category/categorylist";
	}
	
	@ResponseBody
	@RequestMapping(value = "categorylist")
	public String getCategoryList(Model model) {
		String json = null;
		try {
			json = mapper.writeValueAsString(categoryService.listAll("Category"));
			json = "{\"data\":" + json + "}";
		} catch (IOException e) {
			e.printStackTrace();
		}
		return json;
	}

	@RequestMapping(value = "addCategory", method = RequestMethod.GET)
	public String add(Model model) {
		model.addAttribute("bedList", bedService.listAll("Bed"));
		return "category/add";
	}

	@ResponseBody
	@RequestMapping(value = "addCategory", method = RequestMethod.POST)
	public String addCategory(Category category,@RequestParam("category_pic") MultipartFile file, 
			HttpServletRequest request) {
		
		try {
			String path = request.getServletContext().getRealPath("/images/category");// 上传文件路径
			if (!file.isEmpty()) {
				// 上传文件名
				String filename = file.getOriginalFilename();
				File filepath = new File(path, filename);
				// 判断路径是否存在，如果不存在就创建一个
				if (!filepath.getParentFile().exists()) {
					filepath.getParentFile().mkdirs();
				}
				// 将上传文件保存到一个目标文件当中
				file.transferTo(new File(path + File.separator + filename));
				category.setPic("images/category/"+file.getOriginalFilename());
			}
			if (categoryService.save(category))
				return JSONUtil.returnSuccess();
			else
				return JSONUtil.returnFailure();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return JSONUtil.returnFailure();
	}

	@RequestMapping(value = "editCategory", method = RequestMethod.GET)
	public String edit(int categoryid, Model model) {
		Category c=categoryService.findById(Category.class, categoryid);
		model.addAttribute("category", c);
		model.addAttribute("checked",c.getBed().getBedid());
		model.addAttribute("bedList", bedService.listAll("Bed"));
		return "category/edit";
	}

	@ResponseBody
	@RequestMapping(value = "editCategory", method = RequestMethod.POST)
	public String editCategory(Category category) {
		if (categoryService.update(category))
			return JSONUtil.returnSuccess();
		return JSONUtil.returnFailure();
	}

	@ResponseBody
	@RequestMapping(value = "deleteCategory")
	public String deleteCategory(int categoryid) {
		if (categoryService.deleteById("Category",categoryid))
			return JSONUtil.returnSuccess();
		else
			return JSONUtil.returnFailure();
	}
}
